package com.etc.traval.daoimpl;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import com.etc.traval.biz.StrategyBiz;
import com.etc.traval.biz.UserBiz;
import com.etc.traval.bizimpl.StrategyBizImpl;
import com.etc.traval.bizimpl.UserBizImpl;
import com.etc.traval.dao.StrategycomDao;
import com.etc.traval.util.JDBCUtil;

import com.etc.travel.entity.Strategycom;

public class StrategycomDaoImpl implements StrategycomDao{
JDBCUtil util=new JDBCUtil();
	
/**添加攻略评论
 * @author lwh
 * @param 评论
 * @return 是否添加成功 
 * 
 */
@Override
	public boolean addStrategycom(Strategycom strategycom) {
		// TODO Auto-generated method stub

		return util.update("insert into t_strategycom(strategycom_content,strategycom_strategy,strategycom_time,strategycom_user)values(?,?,?,?)", strategycom.getStrategycom_content(),strategycom.getStrategycom_strategy().getStrategy_id(),strategycom.getStrategycom_time(),strategycom.getStrategycom_user().getUser_id())>0;
	}


/**返回该攻略的所有评论
 * @author lwh
 * @return 所有评论
 * @param id：评论id 
 * @param page：当前页面
 * @param size：当前页面大小
 */

@Override
public List<Strategycom> queryallStrategycom(int id, int page, int size) {
	// TODO Auto-generated method stub
	ResultSet rs=util.query("select t.* from(select a.*,rownum as myrow  from t_strategycom a where strategycom_strategy= ? and rownum<=?)t where t.myrow >? order by strategycom_time desc", id,page*size,(page-1)*size);
	List<Strategycom> list=new ArrayList<Strategycom>();
	StrategyBiz sdao=new StrategyBizImpl();
	UserBiz ubiz=new UserBizImpl();
	try {
		while(rs.next()){
			Strategycom str=new Strategycom();
			str.setStrategycom_id(rs.getInt("strategycom_id"));
			str.setStrategycom_content(rs.getString("strategycom_content"));
			str.setStrategycom_strategy(sdao.querySTrategyById(rs.getInt("strategycom_strategy")));
			str.setStrategycom_time(rs.getTimestamp("strategycom_time"));
			str.setStrategycom_user(ubiz.queryUserById(rs.getInt("strategycom_user")));
			list.add(str);
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		
		util.closeAll();
	}
	return list;
}


@Override
public boolean deleteStrategycomByID(int id) {
	// TODO Auto-generated method stub
	return util.update("delete from t_strategycom where strategycom_id=?", id)>0;
	 
}


@Override
public int qurtyallCount(int id) {
	// TODO Auto-generated method stub
	ResultSet rs=util.query("select count(*) as counts from t_strategycom where strategycom_strategy=?", id);
	
	int counts=-1;
	try {
		if (rs.next()){
			counts=rs.getInt("counts");
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		util.closeAll();
	}
	return counts;
}


}
